package com.example.mylibrary.testActivity.util;
import android.content.Context;
import android.content.DialogInterface;
import android.location.LocationManager;
import androidx.appcompat.app.AlertDialog;
import com.blankj.utilcode.util.ToastUtils;
import java.util.List;
public class gpsUtil {
    private static gpsUtil instance;
    LocationManager locationManager;
    private gpsUtil(){ }
    public static gpsUtil getInstance(){
        if(instance == null) {  synchronized (gpsUtil.class) { if (instance == null) instance = new gpsUtil(); }}
        return instance;
    }
    /**
     * 判断设备 是否支持GPS
     * @param context
     * @return true 支持 false不支持
     */
    public boolean hasGPSDevice(Context context) {
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        if ( locationManager == null ){ ToastUtils.showShort("设备不支持GPS");return false; }
        final List<String> providers = locationManager.getAllProviders();
        if ( providers == null ) { ToastUtils.showShort("设备不支持GPS");return false; }
        return providers.contains(LocationManager.GPS_PROVIDER);
    }
    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     * @param context
     * @return true 表示开启
     */
    public  boolean isOPen(Context context) {
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps|| network) {
            return true;
        }
        return false;
    }
    public void openGPS(final Context context , int Icon, String Title, final GPSListener gpsListener) {
        new AlertDialog.Builder(context)
                .setIcon(Icon)
                .setTitle(Title)
                .setMessage("开启定位")
                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) { gpsListener.cancel();dialog.dismiss(); }
                })
                .setPositiveButton("打开", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) { gpsListener.open();dialogInterface.dismiss(); }
                })
                .show();
    }
    public interface GPSListener {
        /**
         * 打开
         */
        void open();
        /**
         * 取消
         */
        void cancel();
    }
//    private static String[] PERMISSIONS_STORAGE = {
//            Manifest.permission.ACCESS_COARSE_LOCATION,
//            Manifest.permission.ACCESS_FINE_LOCATION,
//            Manifest.permission.READ_PHONE_STATE
//    };
//            if ( gpsUtil.getInstance().hasGPSDevice(context)){
//        if (gpsUtil.getInstance().isOPen(context))
//            ToastUtils.showShort("GPS已经打开");
//        else gpsUtil.getInstance().openGPS(context,R.mipmap.ic_launcher,"", new gpsUtil.GPSListener() {
//            @Override
//            public void open() {
//                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//                startActivityForResult(intent,887);
//            }
//            @Override
//            public void cancel() {
//                ToastUtils.showShort("拒绝打开GPS");
//            }
//        });
//    }
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch(requestCode){
//            case 887:
//                ToastUtils.showShort(gpsUtil.getInstance().isOPen(context)?"GPS已经打开":"GPS未打开");
//                break;
//            default:break;
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }
}
